﻿using Microsoft.Practices.Unity;
using Model;
using MyWebTest.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebIOCService.Interface;
using Ymatou.CommonService;
using Ymatou.Coupon.Manage.Contract;

namespace MyWebTest
{
    /// <summary>
    /// TestMyHandler 的摘要说明
    /// </summary>
    public class TestMyHandler : IHttpHandler
    {
        [Dependency]
        public ICouponService CouponService { get; set; }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            ConsumerJob dd = new ConsumerJob();
            dd.OnStart();

            context.Response.Write("Hello World");
        }


        /// <summary>
        /// 获取下载
        /// </summary>
        /// <param name="batchKey"></param>
        /// <param name="batchId"></param>
        /// <param name="type">下载类型(1:兑换码；2:优惠券；3:发放失败用户</param>
        /// <returns></returns>
        string Download(string batchCode, int batchId, int type)
        {
            string msg = "";
            string data = "";
            int code = 1;

            return string.Format("{Code ={0},Msg ={1},Result ={2}}", code, msg, data);
        }

        /// <summary>
        /// 根据批次号获取所有兑换码
        /// </summary>
        /// <param name="batchCode"></param>
        /// <returns></returns>
        List<Coupon> GetConversionCodesByBatchCode(string batchCode, ref string msg)
        {
            List<Coupon> list = new List<Coupon>();
            try
            {
                int conversionBillId = CouponService.GetConversionBillByBatchReq(batchCode);
                if (conversionBillId > 0)
                {
                    int tolCount = 1;
                    var method = CouponService.GetGenerateMethod(conversionBillId);
                    if (method != 1)
                    {
                        var status = CouponService.GetConversionCodeGenerateStatusReq(conversionBillId);
                        if (status != TaskStatusInfo.GenerateComplete)
                        {
                            msg = "兑换码未创建完成，请稍后再试";
                            return list;
                        }
                        tolCount = CouponService.GetGenerateQuantity(conversionBillId);
                    }

                    if (tolCount > 0)
                    {
                        int pageIndex = 1;
                        int pageSize = 1000;
                        if (tolCount > pageSize)
                        {
                            var pageCount = tolCount / pageSize + (tolCount % pageSize > 0 ? 1 : 0);

                            for (var i = 0; i < tolCount; pageIndex++)
                            {
                                var users = CouponService.GetConversionCodesByPaging(conversionBillId, pageIndex, pageSize);
                                if (users != null && users.Count > 0)
                                    list.AddRange(users);
                                i = pageSize * pageIndex;
                            }
                        }
                        else
                        {
                            var users = CouponService.GetConversionCodesByPaging(conversionBillId, pageIndex, tolCount);
                            if (users != null && users.Count > 0)
                                list.AddRange(users);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.Error("CouponService.GetConversionCodesByBatchCode 异常：", ex);
            }
            return list;
        }

        void SysAddOperateLog(object data)
        {
            var obj = (SysOperateLog)data;
            if (obj != null)
            {
                System.IO.File.AppendAllText(string.Format(@"D:\logs\{0}\log.txt", DateTime.Now.ToString("yyyyMMdd")), DateTime.Now.ToString() + "\t" + obj.Msg + "\r\n");
            }
        }

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="paids"></param>
        /// <param name="opUser"></param>
        /// <param name="logtxt"></param>
        /// <returns></returns>
        public void WritOperateLog(string logtxt)
        {
            if (string.IsNullOrEmpty(logtxt)) return;
            var obj = new SysOperateLog
            {
                Msg = logtxt
            };
            System.Threading.Thread task = new System.Threading.Thread(SysAddOperateLog);
            task.Start(obj);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}